//
//  WALoginController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/21.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WALoginController.h"
#import "WARedisterViewController.h"
#import "WAServicesController.h"

@interface WALoginController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property(nonatomic, strong) WALoginViewModel *loginViewModel;

@end

@implementation WALoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    RAC(self.loginViewModel.loginModel,pwd) = self.pwdTF.rac_textSignal;
    RAC(self.loginViewModel.loginModel,account) = self.accountTF.rac_textSignal;
    
    RAC(self.loginBtn,enabled) = self.loginViewModel.enableBtnSignal;
    
    @weakify(self)
    [[self.loginBtn rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [WNTool showLoading];
        [self.loginViewModel.loginCommond execute:@""];
    }];
    [self.loginViewModel.loginCommond.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {

        [WNTool hideLoading];
        [WNTool show:@"登陆成功"];
        
        WNUserInfo *info = [WNUserInfo yy_modelWithJSON:x];
        self.loginManager.userInfo = info;
        [self.loginManager saveUserInfo];
        [self dismissAction:nil];
    }];
    
    [self.loginViewModel.loginCommond.executing subscribeNext:^(NSNumber * _Nullable x) {
        if (x.boolValue) {
            
        }else{
        }
    } error:^(NSError * _Nullable error) {
        NSLog(@"");
    }];

    self.pwdTF.secureTextEntry = YES;
}

- (IBAction)dismissAction:(id)sender {
    if (self.dismissBlock) {
        self.dismissBlock([self.loginManager isloginOnline]);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)serviceAction:(id)sender {
    WAServicesController *VC = [WAServicesController new];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

- (IBAction)registerAction:(id)sender {
    WARedisterViewController *resVC = [WARedisterViewController new];
    [self.navigationController pushViewController:resVC animated:YES];
}
- (IBAction)forgetPassAction:(id)sender {
    WARedisterViewController *VC = [[WARedisterViewController alloc] initWithType:3];
    [self.navigationController pushViewController:VC animated:YES];
}

- (WALoginViewModel *)loginViewModel{
    if (!_loginViewModel) {
        _loginViewModel = [WALoginViewModel new];
    }
    return _loginViewModel;
}
@end
